package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Tag(name = "分类管理")
@Validated
public class CategoryController {

    // @Autowired
    // private Validator validator;

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分类分页查询")
    public Result<PageResult<Category>> page(@Valid CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}",categoryPageQueryDTO);
        PageResult<Category> pageResult = categoryService.pageCategory(categoryPageQueryDTO);
        if (ObjectUtils.isNotEmpty(pageResult)){
            return Result.success(pageResult);
        }else {
            return Result.error(MessageConstant.NOT_FOUND_QUERY);
        }
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @Operation(summary = "新增分类")
    public Result<String> save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        Integer result = categoryService.saveCategory(categoryDTO);
        if (result > 0){
            return Result.success();
        }else{
            return Result.error(MessageConstant.ADD_FAILED);
        }
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @Operation(summary = "根据id删除分类")
    public Result<String> delete(Integer id){
        log.info("删除分类：{}",id);
        Integer result = categoryService.delCategoryById(id);
        if (result > 0){
            return Result.success();
        }else{
            return Result.error(MessageConstant.DELETE_FAILED);
        }
    }

    /**
     * 启用、禁用分类
     * @return
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "启用、禁用分类")
    public Result<String> updStatus(@PathVariable Integer status, Integer id){
        log.info("分类状态：{},分类id：{}", status,id);
        //转换为空对象
        Category category = new Category()
                .setId(Long.valueOf(id))
                .setStatus(status);
        Integer result = categoryService.updStatus(category);
        if (result > 0){
            return Result.success();
        }else{
            return Result.error("操作失败");
        }

    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @Operation(description = "修改分类信息")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类信息：{}", categoryDTO);
        Integer result = categoryService.update(categoryDTO);
        if(result > 0){
            return Result.success();
        }else{
            return Result.error(MessageConstant.EDIT_FAILED);
        }
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @Operation(description = "根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        log.info("根据类型查询分类：{}", type);
        List<Category> categoryList = categoryService.list(type);
        if (CollectionUtils.isNotEmpty(categoryList)){
            return Result.success(categoryList);
        }else {
            return Result.error(MessageConstant.NOT_FOUND_QUERY);
        }
    }








}
